import { defineStore } from 'pinia'

import { login, getInfo } from '@/api/login'
import { getToken, setToken, removeToken } from '@/utils/auth'
// import { getWxConfig } from '@/api/wx'
export const useUserStore = defineStore('user', {
    state: () => ({
        token: getToken(),
        id: '',
        name: '',
        username: '',
        vip: '',
        email: '',
        avatar: '',
        balance: '',
        rewardBalance: '',
        memberEnd: '',
        openId: '',
        strategyList: [],
        info: {},
    }),
    getters: {
        userInfo: (state) => state.info || {},
        strategyAllList:  (state) => state.strategyList,
        isLogin: (state) => !(!state.id),
        ID: (state) => state.id,
        userName: (state) => state.email,
        userEmail: (state) => state.email,
        nickName: (state) => state.name,
        avatarImg: (state) => state.avatar,
        balanceWallet: (state) => state.balance,
        rewardWallet: (state) => state.rewardBalance,
        VIP: (state) => state.vip,
        wxOpenId: (state) => state.openId,
        memberEndTime: (state) => state.memberEnd,
        language: () => localStorage.getItem('lang') || (navigator.language || 'en').toLocaleLowerCase().split('-')[0] || 'en'
    },
    actions: {
        setStrategyList(data) {
            this.strategyList = data;
        },
        setNickName(name) {
            this.name = name
        },
        setAvatar(img) {
            this.avatar = img
        },
        setVip(vip) {
            this.vip = vip
        },

        // 登录
        login(userInfo) {
            const username = userInfo.username.trim()
            return new Promise((resolve, reject) => {
                login({ username: username, password: userInfo.password }).then(response => {
                    const datas = response.data
                    setToken(datas.token)
                    this.token = datas.token
                    this.id = datas.uid
                    this.name = datas.name
                    this.vip = datas.vip
                    this.email = datas.email
                    this.avatar = datas.avatar
                    this.balance = datas.balance
                    this.rewardBalance = datas.reward
                    this.openId = datas.openId
                    this.memberEnd = datas.memberEndTime
                    resolve()
                }).catch(error => {
                    console.log(error)
                    reject(error)
                })
            })
        },

        // 获取用户信息
        getInfo() {
            return new Promise((resolve, reject) => {
                getInfo(this.token).then(response => {
                    const datas = response.data
                    this.id = datas.id
                    this.name = datas.name
                    this.vip = datas.vip
                    this.email = datas.email
                    this.avatar = datas.avatar
                    this.balance = datas.balance
                    this.rewardBalance = datas.reward
                    this.openId = datas.openId
                    this.memberEnd = datas.memberEndTime
                   
                    resolve(response)
                }).catch(error => {
                    console.log(error)
                    reject(error)
                })
            })
        },
        // setWxConfig() {
        //     return new Promise((resolve, reject) => {
        //         console.log('====', location.href)
        //         getWxConfig({
        //             url: 'https://www.aiaabiok.com/me',
        //         }).then(res => {
        //             console.log(res);
        //             if (res.code === 20000) {
        //                 const { appId, timestamp, nonceStr, signature } = res.data;
        //                 wx.config({
        //                     debug: true, // 开发阶段建议打开调试模式
        //                     appId, // 公众号或小程序的 AppID
        //                     timestamp, // 生成签名的时间戳
        //                     nonceStr, // 生成签名的随机串
        //                     signature, // 签名
        //                     jsApiList: ['updateAppMessageShareData'], // 需要使用的JS接口列表
        //                 });
        //             }
        //         }).catch(error => {
        //             console.log(error)
        //             reject(error)
        //         })
        //     })
        // },

        setUserInfo(data) {
            console.log('=======setUserInfo', data);
            this.info = data;
        },

        // 前端 登出
        fedLogOut() {
            return new Promise(resolve => {
                this.id = ''
                this.token = ''
                removeToken()
                resolve()
            })
        },
        resetToken() {
            return new Promise(resolve => {
                this.id = ''
                this.token = ''
                removeToken()
                resolve()
            })
        }
    }
})
